/**
 * Created by Lee on 2019/1/8.
 */
import axios from 'axios'

const state={
    newContent:'',
    newArticleTitle:'',
    articles:[
        {
            title:'第一篇文章的题目',
            id:1
        },
        {
            title:'第2篇文章的题目',
            id:2
        },
        {
            title:'第3篇文章的题目',
            id:3
        }
    ],
    article:{
        id:1,
        title:'第一篇文章的题目',
        content:'这是第一篇文章的内容。'
    }
}
const getters = {
}
const mutations = {
    updateNewContent(state,payload){
        state.newContent = payload;
    },
    updateNewArticleTitle(state,payload){
        state.newArticleTitle = payload;
    }
}
const actions = {
    //保存文章内容
    saveText(context,payload){
        let newContentValidate = payload.common.validate.notEmpty(context.state.newContent,payload,'文章内容不能为空!');
        let newArticleTitleValidate = payload.common.validate.notEmpty(context.state.newArticleTitle,payload,'文章标题不能为空!');
        if(newContentValidate && newArticleTitleValidate){
            payload.saveTextLoading = true;
            axios.post('/index.php/article/index/save',{
                text: context.state.newContent
            }).then((res)=>{
                console.log(res.data);
                payload.saveTextLoading = false;
                payload.$Message.success('保存成功');
            },(err)=>{
                console.log(err)
                payload.saveTextLoading = false;
                payload.$Message.error({
                    content: '保存出错，请联系系统管理员进行查看！',
                    duration: 5,
                    closable: true
                });
            })
        }
    }
}

export default{
    namespaced:true,
    state,
    getters,
    actions,
    mutations
}